//
//  MKUITabBarController.m
//  MKUI
//
//  Created by sushangjin on 16/3/29.
//  Copyright © 2016年 sushangjin. All rights reserved.
//

#import "MKUITabBarController.h"
#import "MKUITabBarItemViewContainerCollectionView.h"
#import "MKUITabBarContentContainerListView.h"
@interface MKUITabBarController()
@property(nonatomic,strong) MKUITabBarItemContainer *itemContainer;
@property(nonatomic,strong) MKUITabBarContentContainer *contentContainer;
@property(nonatomic,assign) Class itemContainerClass;
@property(nonatomic,assign) Class contentContainerClass;
@end

@implementation MKUITabBarController
- (id)init{
	return [self initWithItemContainerClass:[MKUITabBarItemViewContainerCollectionView class] contentContainerClass:[MKUITabBarContentContainerListView class]];
}
- (id)initWithItemContainerClass:(Class)tabBarItemViewContainerClass contentContainerClass:(Class)contentContainerClass{
	if (self=[super init]) {
		self.itemContainerClass = tabBarItemViewContainerClass;
		self.itemContainer = [[self.itemContainerClass alloc] init];
        self.itemContainer.tabBarController = self;
		
		self.contentContainerClass = contentContainerClass;
		self.contentContainer = [[self.contentContainerClass alloc] init];
        self.contentContainer.tabBarController = self;
	}
	return self;
}
- (void)reloadData{
    [self reloadDataWithAnimated:NO];
}
- (void)reloadDataWithAnimated:(BOOL)animated{
    self.contentContainer.contents = self.contents;
    self.contentContainer.selectedIndex = self.selectedIndex;
    [self.contentContainer reloadDataWithAnimated:animated];
    
    self.itemContainer.items = [self items];
    self.itemContainer.selectedIndex = self.selectedIndex;
    [self.itemContainer reloadDataWithAnimated:animated];
}
- (void)setContents:(NSArray<id<MKUITabBarContentProtocol>> *)contents{
    self.contentContainer.contents = contents;
    self.itemContainer.items = [self items];
}
- (NSArray<id<MKUITabBarContentProtocol>> *)contents{
    return self.contentContainer.contents;
}
- (NSArray<UITabBarItem *> *)items{
	return [self.contentContainer tabBarItems];
}
- (void)setSelectedIndex:(NSInteger)index{
    self.itemContainer.selectedIndex = index;
    self.contentContainer.selectedIndex = index;
}
- (id<MKUITabBarContentProtocol>)selectedContent{
    return self.contentContainer.selectedContent;
}
- (NSInteger)selectedIndex{
    NSInteger index = self.contentContainer.selectedIndex;
    return index;
}
- (void)setSelectedContent:(id<MKUITabBarContentProtocol>)selectedContent{
    self.contentContainer.selectedContent = selectedContent;
    self.itemContainer.selectedIndex = self.contentContainer.selectedIndex;
}
- (void)didSelectIndex:(NSInteger)index{
    self.selectedIndex = index;
    if([self.delegate respondsToSelector:@selector(tabBarController:didSelectIndex:)]){
        [self.delegate tabBarController:self didSelectIndex:index];
    }
}
- (void)setSelectedIndex:(NSInteger)index animated:(BOOL)animated{
    [self.contentContainer setSelectedIndex:index animated:animated];
    [self.itemContainer setSelectedIndex:index animated:animated];
    self.selectedIndex = index;
    [self didSelectIndex:index];
}
- (BOOL)shouldSelectIndex:(NSInteger)index{
    if([self.delegate respondsToSelector:@selector(tabBarController:shouldSelectIndex:)]){
        return [self.delegate tabBarController:self shouldSelectIndex:index];
    }
    return YES;
}
- (void)itemContainerDidSelectIndex:(NSInteger)index animated:(BOOL)animated{
    if([self.delegate respondsToSelector:@selector(tabBarController:willSelectIndex:)]){
        [self.delegate tabBarController:self willSelectIndex:index];
    }
    [self.contentContainer setSelectedIndex:index animated:animated];
    [self didSelectIndex:index];
}
- (void)contentContainerDidSelectIndex:(NSInteger)index animated:(BOOL)animated{
    [self.itemContainer setSelectedIndex:index animated:animated];
    [self didSelectIndex:index];
}
- (void)contentContainerDidScrollFrom:(NSInteger)fromIndex to:(NSInteger)toIndex withProgress:(CGFloat)progress{
    [self.itemContainer didScrollFromIndex:fromIndex to:toIndex withProgress:progress];
}
@end

@implementation MKUITabBarController (ContentsUpdate)
- (void)addContent:(id<MKUITabBarContentProtocol>)content{
	if(!content)return;
	NSArray<id<MKUITabBarContentProtocol>> *list = self.contents;
	if([list containsObject:content]){
		return;
	}
	NSMutableArray<id<MKUITabBarContentProtocol>> *contents = [[NSMutableArray alloc] initWithArray:list];
	[contents addObject:content];
	self.contents = contents;
}
- (void)insertContent:(id<MKUITabBarContentProtocol>)content toIndex:(NSInteger)index{
	if(!content)return;
    id<MKUITabBarContentProtocol> selectedContent = self.selectedContent;
    
	NSArray<id<MKUITabBarContentProtocol>> *list = self.contents;
	if([list containsObject:content]){
		return;
	}
	NSMutableArray<id<MKUITabBarContentProtocol>> *contents = [[NSMutableArray alloc] initWithArray:list];
	[contents insertObject:content atIndex:index];
	self.contents = contents;
    
    self.selectedContent = selectedContent;
}
- (void)removeContent:(id<MKUITabBarContentProtocol>)content{
    NSInteger index = [self.contents indexOfObject:content];
    if(index!=NSNotFound){
        [self removeContentAtIndex:index];
    }
}
- (void)removeContentAtIndex:(NSInteger)index{
	NSArray<id<MKUITabBarContentProtocol>> *list = self.contents;
	if(index<0||index>=list.count){
		return;
	}
    id<MKUITabBarContentProtocol> selectedContent = self.selectedContent;
    NSInteger selectedIndex = self.selectedIndex;
	BOOL deleteSelected = index==self.selectedIndex;
	NSMutableArray<id<MKUITabBarContentProtocol>> *contents = [[NSMutableArray alloc] initWithArray:list];
	[contents removeObjectAtIndex:index];
	self.contents = contents;
	if(deleteSelected){
        if(selectedIndex>0){
            selectedIndex--;
        }
        self.selectedIndex = selectedIndex;
    }else{
        self.selectedContent = selectedContent;
    }
}
@end
